package com.qf.web;

import com.qf.model.Banktable;
import com.qf.model.User;
import com.qf.service.DepositService;
import com.qf.service.impl.DepositServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/depositservlet")
public class DepositServlet extends BaseServlet{
   DepositService depositService =  new DepositServiceImpl();

    //存款
    public void deposit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String s = req.getParameter("usercard");   //银行卡号
        String s1 = req.getParameter("amountmoney");  //金额
        String date = req.getParameter("date");  //获取存款期限
        BigDecimal amoutMoney = BigDecimal.valueOf(Integer.parseInt(s1));

        Banktable banktable = new Banktable();  //新建一个存款业务
        banktable.setCid(s);
        banktable.setAmountMoney(amoutMoney);
        banktable.setDate(date);
        //调用业务层
        boolean b = depositService.depositMoney(banktable);
        if (b){
            req.getRequestDispatcher("/deposit/success.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher("/deposit/fail.jsp").forward(req,resp);
        }
    }

    public void findDepositByCid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String s = req.getParameter("cardid");  //获取要取款的银行卡号
        Banktable banktable = new Banktable();  //新建一个银行流水对象用来返回数据
        banktable.setCid(s);
        Banktable banktable1 =  depositService.findDepositByCid(banktable); //通过银行卡号查询存款信息
            //有存款记录
            req.getSession().setAttribute("banktable",banktable1);
            req.getSession().setAttribute("cardId",s);
            resp.sendRedirect("/deposit/current.jsp");


//            //如果没有记录，就返回空的存款记录
//            req.getSession().setAttribute("banktable",banktable);



    }

    //取款
    public void draw(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cardnum = req.getParameter("usercard");  // 获取要取款的银行卡号
        String outmoney = req.getParameter("outmoney");   // 获得要取款的钱

        int row = depositService.drawmoney(cardnum,outmoney);


    }

//    public void findAlldarw(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        User user = req.getSession().getAttribute("user");
//
//
//    }

}
